package cn.loveq.android;

import java.util.Iterator;
import java.util.List;

import cn.loveq.android.R;

import weibo4android.Comment;
import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyTestActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mytestlayout);

		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		// 新建�?��RequestToken
		RequestToken requestToken = new RequestToken(Weibo.CONSUMER_KEY, Weibo.CONSUMER_SECRET);
		// 新建�?��AccessToken
		AccessToken accessToken = new AccessToken("5e3edbe0e4e298eb6d41524fd529f742", "ffeb41e51652cad82f537137b855c2b5");
		// 将RequestToken和AccessToken设到OAuthConstant单例�?
		OAuthConstant.getInstance().setRequestToken(requestToken);
		OAuthConstant.getInstance().setAccessToken(accessToken);

		Button button = (Button) findViewById(R.id.btnaddstatus);
		button.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					TextView tv1 = (TextView) findViewById(R.id.tv1);
					String str = new String();

					// 得到具有授权的weibo实例
					Weibo weibo = OAuthConstant.getInstance().getWeibo();
					weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());

					// 得到微博
					List<Status> pubstat = weibo.getFriendsTimeline();
					for (Iterator<Status> it = pubstat.iterator(); it.hasNext();) {
						Status st = it.next();
						// 微博的所有数�?
						str += st.toString();
						// 只显示谁说了
						// str+=st.getUser().getName()+":"+st.getText();
						str += "\n--------------------------\n";
					}
					tv1.setText(str);

				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		Button button1 = (Button) findViewById(R.id.btnaddstatus1);
		button1.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Weibo weibo = new Weibo();
				Status status = null;
				try {
					// 新建AccessToken
					AccessToken accessToken = new AccessToken("5e3edbe0e4e298eb6d41524fd529f742", "ffeb41e51652cad82f537137b855c2b5");
					weibo.setToken(accessToken.getToken(), accessToken.getTokenSecret());
					status = weibo.updateStatus("测试~这条新浪微博消息！！！"); // 关键是这里，能否成功调用updateStatus发表微博，有兴趣的可以仔细研究一下
				} catch (WeiboException e) {
					e.printStackTrace();
					if (e.getStatusCode() == 400) {
						// 内容重复，新浪微博不允许重复的内容发布 如果内容重复会在这里抛出异常
					} else if (e.getStatusCode() == 403) {
						// 帐号密码错误
					}
					return;

				}
				System.out.println(status.getId() + " : " + status.getText() + " " + status.getCreatedAt());
				// 如果程序能执行到这里，证明已经发表成功，下面是执行成功后跳转到另外一个页面�?
				Intent intent = new Intent();
				intent.setClass(MyTestActivity.this, AndroidExample.class);
				MyTestActivity.this.startActivity(intent);

			}

		});
		
		Button button2 = (Button) findViewById(R.id.btnaddstatus2);
		button2.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				  try {
			        	Weibo weibo = new Weibo();
			        	// 新建AccessToken
						AccessToken accessToken = new AccessToken("5e3edbe0e4e298eb6d41524fd529f742", "ffeb41e51652cad82f537137b855c2b5");
						weibo.setToken(accessToken.getToken(), accessToken.getTokenSecret());
			        	List<Status> list = weibo.getUserTimeline("1738789264");
			        	if(list.size() > 0) {
			        		//最新一条微博信息id
			        		String sid = list.get(0).getId()+"";
			        		List<Comment> comments = weibo.getComments(sid);
			        		if(comments.size() > 0) {
			        			String cid = comments.get(0).getId()+"";//评论的id
			        			Comment status = weibo.reply(sid, cid, "哈哈");//args[3]：回复内容
			        			System.out.println(status.toString());
			        		}
			        	}
			        	
//						Status status = weibo.updateStatus("test.....");
//			        	Thread.sleep(1000);
//			        	String sid = status.getId()+"";
//			        	Comment comment = weibo.updateComment("1", sid, null);
//			        	System.out.println(comment.getId() + " : " + comment.getText() + "  " + comment.getCreatedAt());
//			        	Thread.sleep(1000);
//			        	comment = weibo.updateComment("2", sid, null);
//			        	System.out.println(comment.getId() + " : " + comment.getText() + "  " + comment.getCreatedAt());
//			        	Thread.sleep(1000);
//			        	comment = weibo.updateComment("3", sid, null);
//			        	System.out.println(comment.getId() + " : " + comment.getText() + "  " + comment.getCreatedAt());
//			        	Thread.sleep(1000);
//			        	comment = weibo.updateComment("4", sid, null);
//			        	System.out.println(comment.getId() + " : " + comment.getText() + "  " + comment.getCreatedAt());
//			        	Thread.sleep(1000);
//			        	comment = weibo.updateComment("5", sid, null);
//			        	System.out.println(comment.getId() + " : " + comment.getText() + "  " + comment.getCreatedAt());
//			        	Thread.sleep(1000);
//			        	comment = weibo.updateComment("6", sid, null);
//			        	System.out.println(comment.getId() + " : " + comment.getText() + "  " + comment.getCreatedAt());
						
					} catch (Exception e) {
						e.printStackTrace();
					}
			}
			
		});
	}
	
	
}
